library(sf)
library(tidyverse)
library(ragg)

# this code was written before the release of s2
sf_use_s2(FALSE)

asm_sites_cod <- st_read("Data/IPIS/cod_mines_curated_all_opendata_p_ipis.shp")

world <- st_read("Data/GADM/gadm36_levels.gpkg", "level1") %>% 
          st_transform(st_crs(asm_sites_cod)) #it's already in 4326 but slight differences in WKT causes error

congo <- world %>% filter(NAME_0 == "Democratic Republic of the Congo")

congo_provinces_of_interest <- c("Ituri",
                                 "Sud-Kivu",
                                 "Nord-Kivu",
                                 "Haut-Katanga",
                                 "Maniema",
                                 "Tanganyika",
                                 "Haut-Lomami",
                                 "Tshopo")

congo_provinces_of_interest <- congo %>% 
  dplyr::filter(NAME_1 %in% congo_provinces_of_interest)

mining_permits_cod <- st_read("Data/Mining Permits/Democratic_Republic_of_the_Congo_mining_permits.shp")

mining_permits_cod_filtered <-
  mining_permits_cod %>% 
  filter(grepl("Actif", statut)) %>% 
  filter(grepl("Au|Ta|Co|Cu|Diamant|Sn|W", resource))

agg_png("./Figures/Figure-A-2-IPIS-and-mining-permits.png", 
        width = 14, height = 14, res = 700, units = "in")
print(
ggplot() +
  geom_sf(data = congo) + 
  geom_sf(aes(fill = "Training Area"), data = congo_provinces_of_interest)+
  geom_sf(aes(color = "Mining Concession"),data = mining_permits_cod_filtered, fill = NA) + 
  geom_sf(aes(shape = "ASM"), data = asm_sites_cod, color = "orange", alpha = .2) + 
  labs(title = "Congo ASM points and Mining Concessions", color = "", fill = "", shape = "") +
  scale_colour_manual(values = c("darkred")) + 
  scale_fill_manual(values = c("grey20"))
)
dev.off()
